package com.yunhe.authority.controller.authority;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.yunhe.authority.domain.authority.Firm;
import com.yunhe.authority.domain.authority.Project;
import com.yunhe.authority.domain.authority.ProjectData;
import com.yunhe.authority.domain.authority.Station;
import com.yunhe.authority.service.authority.StationService;
import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * 电站Controller
 * @author liuronglei
 */
@Api(value = "station",description = "项目维护", tags = {"Authority_Station"})
@RestController
@RequestMapping("/stations")
public class StationController {

    @Autowired
    private StationService stationService;

    /**
     * 新增电站
     * @param station 电站对象
     */
    @ApiOperation("新增项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "单位ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.POST)
    @OperationRecord(menu = "电站管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> post(@RequestBody Station station,
                                             @RequestParam("firmId") Long firmId)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, PinyinException, IOException {
        Station newStation = stationService.createStation(station, firmId);
        return new ResponseEntity<>(new ResultObject<>(newStation), HttpStatus.OK);
    }

    /**
     * 根据电站ID删除电站
     * @param id 电站ID
     */
//    @ApiOperation("根据电站ID删除电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    @OperationRecord(menu = "电站管理", actionType = SystemConsts.ACTION.DELETE)
//    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
//        stationService.deleteStation(id);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }

    /**
     * 更新电站（更新全部字段）
     * @param id 电站ID
     * @param station 电站对象
     */
//    @ApiOperation("更新电站（更新全部字段）")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path"),
//            @ApiImplicitParam(name = "firmCode", value = "单位代码", dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
//    @OperationRecord(menu = "电站管理", actionType = SystemConsts.ACTION.EDIT)
//    public ResponseEntity<ResultObject> put(@PathVariable("id") Long id,
//                                            @RequestBody Station station,
//                                            @RequestParam("firmCode") Optional<Long> firmCode)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        station.setId(id);
//        Station newStation = stationService.updateStation(firmCode.orElse(null), station);
//        return new ResponseEntity<>(new ResultObject<>(newStation), HttpStatus.OK);
//    }

    /**
     * 更新电站（更新指定字段）
     * @param id
     * @return
     * @throws ArgumentErrorException
     * @throws ObjectNotFoundException
     * @throws UniqueConstraintsException
     */
    @ApiOperation("更新电站（更新指定字段）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType="long", paramType = "path"),
            @ApiImplicitParam(name = "firmId", value = "单位id", dataType="long", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    @OperationRecord(menu = "电站管理", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam(value = "firmId",required = true) Optional<Long> firmId,
                                              @RequestBody Project project)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException, IOException {
        Station newStation = stationService.updateStation(id, firmId.orElse(null),project);
        return new ResponseEntity<>(new ResultObject<>(newStation), HttpStatus.OK);
    }

    /**
     * 根据电站ID获得电站
     * @param id 电站ID
     */
    @ApiOperation("根据电站ID获得电站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        Station station = stationService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(station), HttpStatus.OK);
    }

//    /**
//     * 根据电站代码获得电站
//     * @param code 电站代码
//     */
//    @ApiOperation("根据电站代码获得电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "code", value = "电站代码", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/byCode/{code}", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getByCode(@PathVariable("code") Long code) {
//        Station station = stationService.findByCode(code);
//        return new ResponseEntity<>(new ResultObject<>(station), HttpStatus.OK);
//    }

//    /**
//     * 根据条件获得电站列表
//     */
//    @ApiOperation("根据条件获得电站列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "firmCode", value = "单位代码", dataType = "long", paramType = "query"),
//            @ApiImplicitParam(name = "typeCode", value = "站点类型代码", dataType = "long", paramType = "query"),
//            @ApiImplicitParam(name = "queryStr", value = "查询条件", dataType = "string", paramType = "query")
//    })
//    @RequestMapping(method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> search(@RequestParam("firmCode") Optional<Long> firmCode,
//                                               @RequestParam("typeCode") Optional<Long> typeCode,
//                                               @RequestParam("queryStr") Optional<String> queryStr,
//                                               @ModelAttribute PageParam pageParam) {
//        Iterable<Station> results = stationService.findStations(firmCode.orElse(null), typeCode.orElse(null),
//                queryStr.orElse(null), pageParam);
//        Long totalCount = stationService.findTotalCount(firmCode.orElse(null), typeCode.orElse(null),
//                queryStr.orElse(null));
//        Pageable pageable = QueryUtil.getPageRequest(pageParam);
//        Page page = new PageImpl((List<Station>)results, pageable, totalCount);
//        return new ResponseEntity<>(new ResultObject<>(page), HttpStatus.OK);
//    }

    /**
     * 绑定电站拥有的资源
     * @param id 电站ID
     * @param type 资源类型
     * @param resourceId 资源ID
     */
//    @ApiOperation("绑定电站拥有的资源")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path"),
//            @ApiImplicitParam(name = "type", value = "资源类型", required = true, dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "resourceId", value = "资源ID", required = true, dataType = "string", paramType = "query")
//    })
//    @RequestMapping(value = "/{id}/resources", method = RequestMethod.POST)
//    public ResponseEntity<ResultObject> bindResource(@PathVariable("id") Long id,
//                                                     @RequestParam("type") String type,
//                                                     @RequestParam("resourceId") Long resourceId)
//            throws ArgumentErrorException {
//        stationService.bindResource(id, type, resourceId);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }

    /**
     * 解绑电站拥有的资源
     * @param id 电站ID
     * @param resourceId 菜单ID
     */
//    @ApiOperation("解绑电站拥有的资源")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path"),
//            @ApiImplicitParam(name = "resourceId", value = "资源ID", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/{id}/resources", method = RequestMethod.DELETE)
//    public ResponseEntity<ResultObject> unbindResource(@PathVariable("id") Long id,
//                                                       @RequestParam("resourceId") Long resourceId) {
//        stationService.unbindResource(id, resourceId);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }

    /**
     * 更新电站绑定的资源
     * @param id 电站ID
     * @param domainList 资源列表
     */
//    @ApiOperation("更新电站绑定的资源")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/{id}/resources", method = RequestMethod.PUT)
//    public ResponseEntity<ResultObject> updateResource(@PathVariable("id") Long id,
//                                                       @RequestBody DomainList<ResourceTree> domainList)
//            throws DatabaseProcessException {
//        stationService.updateResource(id, domainList);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }

    /**
     * 根据电站ID获得资源树
     * @param id 电站ID
     * @param type 类型列表
     */
//    @ApiOperation("根据电站ID获得资源树")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path"),
//            @ApiImplicitParam(name = "type", value = "类型（以逗号间隔）", dataType = "string", paramType = "query")
//    })
//    @RequestMapping(value = "/{id}/resources", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getResources(@PathVariable("id") Long id,
//                                                     @RequestParam("type") Optional<List<String>> type) {
//        Iterable<ResourceTree> result = stationService.findResourceTreeById(id, type.orElse(null));
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }

    /**
     * 更新电站关联的电站类型
     * @param id 电站ID
     * @param stationTypeId 电站类型ID
     */
//    @ApiOperation("更新电站关联的电站类型")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true,dataType = "long", paramType = "path"),
//            @ApiImplicitParam(name = "stationTypeId", value = "电站类型ID", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/{id}/stationTypes", method = RequestMethod.PUT)
//    public ResponseEntity<ResultObject> updateStationType(@PathVariable("id") Long id,
//                                                          @RequestParam("stationTypeId") Long stationTypeId)
//            throws ObjectNotFoundException {
//        stationService.updateStationTypeById(id, stationTypeId);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }

    /**
     * 根据电站获得电站类型
     * @param id ID
     */
//    @ApiOperation("根据电站获得电站类型")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "电站ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/{id}/stationTypes", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getStationType(@PathVariable("id") Long id) {
//        StationType stationType = stationService.findStationTypeById(id);
//        return new ResponseEntity<>(new ResultObject<>(stationType), HttpStatus.OK);
//    }

    /**
     * 根据电站类型获得电站
     * @param stationTypeId 电站类型ID
     */
//    @ApiOperation("根据电站类型获得电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "stationTypeId", value = "电站类型ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/byStationType/{stationTypeId}", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getStationsByStationType(@PathVariable("stationTypeId") Long stationTypeId) {
//        Iterable<Station> results = stationService.findStationsByStationTypeId(stationTypeId);
//        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
//    }

    /**
     * 判断设备是否绑定电站
     * @param devIdList 设备ID列表
//     */
//    @ApiOperation("判断设备是否绑定电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "devIdList", value = "设备ID列表", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/isBindStation", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> isBindStation(@RequestParam("devIdList") List<Long> devIdList) {
//        Map<Long, Boolean> results = stationService.isBindStation(devIdList);
//        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
//    }

    /**
     * 根据设备代码查找所属电站
     * @param devCode 设备代码
     */
//    @ApiOperation("根据设备代码查找所属电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "devCode", value = "设备代码", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/byDevCode", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findStationByDevCode(@RequestParam("devCode") Long devCode) {
//        Station result = stationService.findStationByDevCode(devCode);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }

//    @ApiOperation("查找单位下未被电站绑定的设备")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "firmId", value = "单位ID", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/findDevicesUnbindStation", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findDevicesUnbindStation(@RequestParam("firmId") Long firmId) {
//        Iterable<ResourceTree> results = stationService.findDevicesUnbindStation(firmId);
//        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
//    }

//    @ApiOperation("根据单位ID查找电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "firmId", value = "单位ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/byFirm/{firmId}", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findByFirmId(@PathVariable("firmId") Long firmId) {
//        Iterable<Station> result = stationService.findByFirmId(firmId);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    @ApiOperation("根据角色ID查找有权限的电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/byRole/{roleId}", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findByRoleId(@PathVariable("roleId") Long roleId) {
//        Iterable<Station> result = stationService.findByRoleId(roleId);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    @ApiOperation("根据角色ID查找可选的电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long", paramType = "path")
//    })
//    @RequestMapping(value = "/byRole/optional/{roleId}", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findOptionalByRoleId(@PathVariable("roleId") Long roleId) {
//        Iterable<Station> result = stationService.findOptionalByRoleId(roleId);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    @ApiOperation("根据电站标题查找电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "title", value = "电站标题", required = true, dataType = "string", paramType = "query")
//    })
//    @RequestMapping(value = "/byTitleContaining", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findByTitleContaining(@RequestParam("title") String title) {
//        Iterable<Station> result = stationService.findByTitleContaining(title);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    @ApiOperation("根据电站容量查找电站")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "capacity", value = "电站容量", required = true, dataType = "string", paramType = "query")
//    })
//    @RequestMapping(value = "/byCapacity", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findByCapacity(@RequestParam("capacity") Double capacity) {
//        Iterable<Station> result = stationService.findByCapacity(capacity);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    /**
//     * 根据代码列表获得电站列表
//     * @param codeList 代码列表
//     */
//    @ApiOperation("根据代码列表获得电站列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "codeList", value = "代码列表（以逗号间隔）", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/byCodes", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> findByCodes(@RequestParam("codeList") List<Long> codeList) {
//        Iterable<Station> result = stationService.findStationByCodes(codeList);
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }
//
//    @ApiOperation("根据角色和单位获得电站列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long", paramType = "query"),
//            @ApiImplicitParam(name = "firmId", value = "单位ID", required = true, dataType = "long", paramType = "query")
//    })
//    @RequestMapping(value = "/byRoleAndFirm", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getStationsByUserAndFirm(@RequestParam("roleId") Long roleId,
//                                                                 @RequestParam("firmId") Long firmId) {
//        Iterable<Station> stations = stationService.getStationsByRoleAndFirm(roleId, firmId);
//        return new ResponseEntity<>(new ResultObject<>(stations), HttpStatus.OK);
//    }

    /**
     * 获取当前登录人所在的园区或下面所有的园区
     * @param
     */
    @ApiOperation("根据条件查询当前登录人满足条件的项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "单位id", required = false, dataType="long", paramType = "path"),
            @ApiImplicitParam(name = "stationId", value = "园区id",required = false, dataType="long", paramType = "path")
    })
    @RequestMapping(value = "/findStationByLoginUser", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findStationByLoginUser(@RequestParam(value = "firmId",required = false) Long firmId,
                                                               @RequestParam(value = "firmId",required = false) Long stationId
                                                                ) throws ArgumentErrorException {
        List<ProjectData> maps =  stationService.findStationByLoginUser(firmId,stationId);
        return new ResponseEntity<>(new ResultObject<>(maps), HttpStatus.OK);
    }

    /**
     * 根据系统标识获得单位
     * @param system 单位ID
     */
    @ApiOperation("根据系统标识获得单位/园区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "system", value = "系统标识", required = true, dataType = "String", paramType = "path")
    })
    @RequestMapping(value = "/getAllFirmAndStation/{system}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getAllFirmAndStation(@PathVariable("system") String system) {
        List<Map<String,Object>> maps = stationService.getAllFirmAndStation(system);
        return new ResponseEntity<>(new ResultObject<>(maps), HttpStatus.OK);
    }

}
